import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const route = new Router({
  routes: [
    {
      path: '/named',
      name: 'default',
      component: () => import('@/components/Hello.vue')
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/components/Login.vue')
    },
    {
      path: '/main',
      name: 'main',
      component: () => import('@/components/Main.vue')
    },
    {
      path: '/main',
      name: 'home',
      component: () => import('@/components/Hello.vue')
    },
    {
      path: '/',
      name: 'root',
      redirect: '/login'
    }
  ]
})
// 添加路由守卫
route.beforeEach((to, from, next) => {
  const isLogin = !!localStorage.jwtToken
  // console.log(isLogin)
  if (to.path === '/login' || to.path === '/register' || to.path === '/') {
    next()
  } else {
    isLogin ? next() : next('/login')
  }
})

export default route
